#coding=utf-8
import ddt
import unittest
import sys
# sys.path.append('E:\\Teacher\\Imooc\\SeleniumPython')
import traceback
from business.register_business import RegisterBusiness
from selenium import webdriver
from HTMLTestRunner_api import HTMLTestRunner
from util.excel_util import ExcelUtil as ex
import os
from log.user_log import UserLog as log
import time
from util.read_ini import ReadIni

data = ex().get_data()
logger = log().get_log()
@ddt.ddt
class FirstDdtCase(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.get(ReadIni().get_value('url'))
        cls.driver.implicitly_wait(2)
        cls.login = RegisterBusiness(cls.driver)

    def tearDown(self):
        time.sleep(1)
        #if sys.exc_info()[0]:
        for method_name,error in self._outcome.errors:
              if error:
                  case_name = self._testMethodName
                  file_path = os.path.join(os.getcwd()+"/report/"+case_name+".png")
                  self.driver.save_screenshot(file_path)
        #print("这个是case的后置调键1")

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    '''
    @ddt.data(
            ['12','Mushishi01','111111','code','user_email_error','请输入有效的电子邮件地址'],
            ['@qq.com','Mushishi01','111111','code','user_email_error','请输入有效的电子邮件地址'],
            ['12@qq.com','Mushishi01','111111','code','user_email_error','请输入有效的电子邮件地址']
        )
    @ddt.unpack
    '''
    @ddt.data(*data)
    def test_register_case(self,data):
        username,code,password_1,password_2,assertCode,assertText = data
        email_error = self.login.register_function(username,code,password_1,password_2,assertCode,assertText)
        self.assertTrue(email_error,"测试失败")

if __name__ == '__main__':
    # file_path = os.path.join(os.getcwd()+"\\report\\"+"first_case1.html")
    # file_path = 'F:\\A_RuanJian\\Py_project\\Mushishi_selenium\\report\\first_case1.html'
    dir01 = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
    file_path = os.path.join(dir01+"\\report\\"+"first_case1.html")
    # print('路径：'+file_path)
    f = open(file_path,'wb')
    suite = unittest.TestLoader().loadTestsFromTestCase(FirstDdtCase)
    runner = HTMLTestRunner(stream=f,
                            title="This is first report1",
                            description="这个是我们第一次测试报告",
                            verbosity=2,save_last_try=True)
    runner.run(suite)
    # unittest.main()
